<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login" v-if="show_s==false">
		<el-form-item prop="user">
			<el-input v-model="form.user" prefix-icon="el-icon-user" clearable :placeholder="$t('login.userPlaceholder')">
				<!-- <template #append>
					<el-select v-model="userType" style="width: 130px;">
						<el-option :label="$t('login.admin')" value="admin"></el-option>
						<el-option :label="$t('login.user')" value="user"></el-option>
					</el-select>
				</template> -->
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="form.password" prefix-icon="el-icon-lock" clearable show-password :placeholder="$t('login.PWPlaceholder')"></el-input>
		</el-form-item>
		<el-form-item style="margin-bottom: 10px;">
				<el-col :span="12">
					<el-checkbox :label="$t('login.rememberMe')" v-model="form.autologin"></el-checkbox>
				</el-col>
				<el-col :span="12" class="login-forgot">
					<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
				</el-col>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
	</el-form>

	<div v-if="show_s">
		 <div class="shopbox" v-for="(item,index) in shop" :key="index" @click="enter(item.token)">
				<h2>{{item.name}}</h2>
				<p style="padding-top: 8px;color:#888">到期时间：{{item.end_time}}</p>
				<p style="padding-top: 8px;">
					<el-tag type='success'>专业版</el-tag>
					<svg t="1728529918795"  class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6318" width="25" height="25"><path d="M881 562H81c-27.6 0-50-22.4-50-50s22.4-50 50-50h800c27.6 0 50 22.4 50 50s-22.4 50-50 50z" p-id="6319"></path><path d="M907.6 540.7L695.5 328.6c-19.5-19.5-19.5-51.2 0-70.7s51.2-19.5 70.7 0L978.4 470c19.5 19.5 19.5 51.2 0 70.7-19.6 19.6-51.2 19.6-70.8 0z" p-id="6320"></path><path d="M695.5 695.4l212.1-212.1c19.5-19.5 51.2-19.5 70.7 0s19.5 51.2 0 70.7L766.2 766.1c-19.5 19.5-51.2 19.5-70.7 0s-19.5-51.2 0-70.7z" p-id="6321"></path></svg>

				</p>
		 </div>
		 <p v-show="shop.length == 0" style="color:#999">暂未开通店铺</p>
	</div>

</template>

<script>
	export default {
		data() {
			return {
				shop:[],
				show_s:false,
				userType: 'admin',
				form: {
					user: "admini",
					password: "123456",
					autologin: false
				},
				rules: {
					user: [
						{required: true, message: this.$t('login.userError'), trigger: 'blur'}
					],
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					]
				},
				islogin: false,
			}
		},
		watch:{
			userType(val){
				if(val == 'admin'){
					this.form.user = 'admin'
					this.form.password = 'admin'
				}else if(val == 'user'){
					this.form.user = 'user'
					this.form.password = 'user'
				}
			}
		},
		mounted() {

		},
		methods: {
			async getMenu(){
				//获取菜单
				var menu = await this.$API.system.universal.list.get({},'/system/menu')
				if(menu.code == 200){
					if(menu.data.menu.length==0){
						this.islogin = false
						this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
							type: 'error',
							center: true
						})
						return false
					}
					this.$TOOL.data.set("MENU", menu.data.menu)
					this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)
					this.$TOOL.data.set("DASHBOARDGRID", menu.data.dashboardGrid)
				}else{
					this.islogin = false
					this.$message.warning(menu.message)
					return false
				}
				this.$router.replace({
					path: '/'
				})
				// this.$message.success("Login Success 登录成功")
				this.islogin = false
			},
			//进入店铺
			async enter(shopid = ''){
				var data = {
					username: this.form.user,
					password: this.form.password,
					shopid:shopid,
					act:'enter'
				}
				//获取token
				var user = await this.$API.system.universal.handle.post(data,'/login/syslogin')
				if(user.code == 200){
					this.$TOOL.cookie.set("TOKEN", user.data.userinfo.token, {
						expires: this.form.autologin? 24*60*60*5 : 0
					})
					this.$TOOL.data.set("USER_INFO", user.data.userinfo)
					this.getMenu()
				}else{
					this.islogin = false
					this.$message.warning(user.message)
					return false
				}

			},
			//账号登录
			async login(){

				var validate = await this.$refs.loginForm.validate().catch(()=>{})
				if(!validate){ return false }

				this.islogin = true
				var data = {
					username: this.form.user,
					password: this.form.password
				}
				//获取token
				var user = await this.$API.system.universal.handle.post(data,'/login/syslogin')
				if(user.code == 200){
					this.islogin = false
					if(user.direct == 1){
						this.$TOOL.cookie.set("TOKEN", user.data.userinfo.token, {
							expires: this.form.autologin? 24*60*60*5 : 0
						})
						this.$TOOL.data.set("USER_INFO", user.data.userinfo)
						this.getMenu()
						return;
					}
					this.shop = user.data.userinfo.shop
					this.show_s = true
				}else{
					this.islogin = false
					this.$message.warning(user.message)
					return false
				}



			},
		}
	}
</script>

<style>
.shopbox{
   width: 100%;
   float: left;
   border-radius: 5px;
   border:1px solid #eee;
   padding: 15px;
   margin-bottom: 15px;
   cursor: pointer;
}
.shopbox:hover{
	border-color: #409EFF;
	.icon{
		 display: block;
		 fill: #409EFF; /* 改变填充颜色 */
		  stroke: #409EFF; /* 如果有描边的话，可以改变描边颜色 */
	}
}
.shopbox .icon{
	float: right;margin-top: -3px;
	display: none;
}
</style>
